package com.example.ytyproject.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.ytyproject.config.SecurityUtil;
import com.example.ytyproject.entity.Menu;
import com.example.ytyproject.entity.User;
import com.example.ytyproject.entity.vo.R;
import com.example.ytyproject.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {

    @Autowired
    private MenuService menuService;

    @GetMapping("/list")
    public R list() {
        List<Menu> list = menuService.listMenus();
        Subject subject = SecurityUtils.getSubject();
        return R.ok(list);
    }

    @GetMapping("/tree")
    public R tree(@Param("roleId") String roleId) {
        return R.ok(menuService.tree(roleId));
    }

    @PostMapping("/addData")
    public R addData(@RequestBody Menu param) {
        log.info("menu addData param: {}", JSONObject.toJSONString(param));
        menuService.addData(param);
        return R.ok();
    }

    @PostMapping("/updateData")
    public R updateData(@RequestBody Menu param) {
        log.info("menu updateData param: {}", JSONObject.toJSONString(param));
        menuService.updateData(param);
        return R.ok();
    }
}
